import java.io.IOException;
import java.util.Scanner;

/**
 * The driver class
 * @author ldong
 */
public class Driver
{
	 
	 public static Scanner scan = new Scanner(System.in);
	 public static AdjacencyList list = new AdjacencyList();;

	 /**
	  * Main method 
	  * @param args
	  */
     public static void main(String[] args)
     {
    	 
		// ask user for a graph file
		System.out.println("Do you wanna run the program!? ");
  		System.out.print(" press 1 to continue, 2 to quit -> ");
  		int done = scan.nextInt();
    	
  		// if not done, keep looping 
  		while(done == 1)
  		{
  			int option = 1;
  			
	  		// if user want to play 
  		  	if(option == 1)
  		  	{
  		  		askAdj(); // get the adjacency list 
  			 }
  		  	else 
  		  	{
  		  		System.out.println("Byebye!");
  		  		done = 0; // exit out the program
  		  	}

  		  	// ask for the algorithm for the new list at the first time
		     ask4Algorithm(list);
		     
			do{
		  		 System.out.print("(1) Do you want to perform a different " +
		  		 		"algorithm \n(2) or start a new adjacency list " +
		  		 		"\n(3) to quit-> ");
		  		 option = scan.nextInt();
		  		 if(option == 1)
		  			 ask4Algorithm(list);
		  		 else if(option == 2)
		  		 {}
		  		 else
		  		 {
		  			 System.out.println("Byebye");
		  			 return;
		  		 }
			}while(option == 1);
			
			System.out.println("Do you wanna run the program!? ");
	  		System.out.print("Press 1 to continue, 2 to quit -> ");
	  		done = scan.nextInt();

  		}
  		
	  		System.out.println("Byebye!");
	  		scan.close(); // close the scanner 
    }
     
		// ask for adjacency list
		public static void askAdj()
		{
			   // ask user to enter input file
				try {
					list.enterInput();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				// print out the adjacent list
		        System.out.println("Your AdjacencyList is showing as below");
		        System.out.print(list);
		}
		
		
		/**
		 *  Ask user to choose algorithm and start Vertex 
		 * @param list
		 */
		public static void ask4Algorithm(AdjacencyList list)
		{

	        // ask user to pick a algorithms
	        System.out.println("1) for BFS");
	        System.out.println("2) for DFS");
	        System.out.println("3) for Dijkstra");
	        System.out.println("4) for Prim");

  			System.out.print("Please enter coorsponding algorithm's number"
  					+"\nwhich you want to run. i.e. 1 for BFS -> ");
  			
  			int alg = scan.nextInt();
	        
			/**
			 * Algorithm Generators
			 */
  			DataStructure<Record> ds;
  			String algorithm = "";
		        if(alg == 1)
		        {
		        	// BFS
		        	ds = new MyQ<Record>();
		        	algorithm = " BFS ";
		        }
		        else if (alg == 2)
		        {
		        	// DFS
		        	ds = new MyStack<Record>();
		        	algorithm = " DFS ";
		        }
	        	else if (alg == 3)
	        	{
	        		// Dijkstra
	        		ds= new MyPQ<Record>(new DijkstraComparator());
	        		algorithm = " Dijkstra ";
	        	}
        		else if (alg == 4)
        		{  
        			// Prim
        			ds= new MyPQ<Record>(new PrimComparator());
        			algorithm = " Prim ";
        		}
        		else
        		{
        			System.out.println("Invalid Option! I will assume" +
        					" you wanna do BFS. ");
			  		ds = new MyQ<Record>();
        		}
		    
		        System.out.print("Please enter a valid vertex" +
		        		"\n if vertex is not found, assume its A-> ");
		        
		        String start = "";
		        if(scan.hasNext()) // reading next line
		        	start = scan.next().toUpperCase();
		        
		        // if not found, pick A
		        if(list.getSpecificVertex(start) == null)
		        	start = "A";
		        
		        System.out.println("The Starting Vertex is " + start);
		        
		        GraphTraverser graph = new GraphTraverser(ds,list,start);
			
		        // ask user for outputs option
		        System.out.print(" Path List(1) or Edge List(2) or Both(3)? -> ");
		        int choice = scan.nextInt();
		        printOptions(choice, graph, algorithm);
		}
		
		/**
		 * Ask for output options
		 * @param option
		 * @param graph
		 * @param algorithm
		 */
		public static void printOptions(int option, GraphTraverser graph, String algorithm)
		{
			System.out.println(algorithm + "result is showing below"); 

			if ( option == 1) // printPaths
				graph.printPaths();
			else if(option ==2)
				graph.printEdge();
			else
			{
				graph.printPaths();
			    graph.printEdge();
			}

		}

}

